# -*- coding: utf-8 -*-
import win32gui
from PIL import ImageGrab, Image, ImageChops, ImageWin
import time

def main():
    # 获取图像
    hwnd = win32gui.FindWindow(0, u'大家来找茬')
    if hwnd:
        win32gui.ShowWindow(hwnd, win32con.SW_RESTORE)
        win32gui.SetForegroundWindow(hwnd)
        rect = win32gui.GetWindowRect(hwnd)
        img = ImageGrab.grab(rect)
    else:
        rect = (0, 0, 0, 0)
        filename = u'test.png'
        img = Image.open(filename)

    # 分割图像
    left_pos_x, left_pos_y = 9, 201  # 左边图位置
    right_pos_x, right_pos_y = 518, 201  # 右边图位置
    w, h = 498, 448  # 图大小
    left_rect = (left_pos_x, left_pos_y, left_pos_x + w, left_pos_y + h)
    right_rect = (right_pos_x, right_pos_y, right_pos_x + w, right_pos_y + h)
    left_img = img.crop(left_rect)
    right_img = img.crop(right_rect)

    # 比较并显示
    hdc = win32gui.GetDC(0)
    dib = ImageWin.Dib(ImageChops.difference(left_img, right_img))
    while True:
        rt = (rect[0] + left_pos_x, rect[1] + left_pos_y, rect[0] + left_pos_x + w, rect[1] + left_pos_y + h)
        dib.draw(hdc, rt)
        time.sleep(0.01)

if __name__ == '__main__':
    main()
